package com.mindcraft.shop.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mindcraft.base.model.MindCraftResult;
import com.mindcraft.base.utils.CommonConst;
import com.mindcraft.base.utils.MindCraftEnum;
import com.mindcraft.shop.po.Category;
import com.mindcraft.shop.po.SkuDetail;
import com.mindcraft.shop.po.SkuInfo;
import com.mindcraft.shop.service.CategoryService;
import com.mindcraft.shop.service.SkuDetailService;
import com.mindcraft.shop.service.SkuInfoService;
import com.mindcraft.shop.vo.CommonSkuVo;
import com.mindcraft.shop.vo.SkuInfoQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(value = "shop", tags = "商城首页接口")
@RestController
@RequestMapping("/shop/home")
public class HomeController {


    @Autowired
    private SkuInfoService skuInfoService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SkuDetailService skuDetailService;

    /**
     * 查询商品List
     */
    @ApiOperation("sku列表")
    @GetMapping("getskuList/{page}/{limit}")
    public MindCraftResult getskuList(@PathVariable Long page,
                                      @PathVariable Long limit,
                                      SkuInfoQueryVo skuInfoVo) {
        Page<SkuInfo> pageParam = new Page<SkuInfo>(page, limit);
        IPage<SkuInfo> pageModel = skuInfoService.selectPageSkuInfo(pageParam, skuInfoVo);
        return MindCraftResult.success(pageModel);

    }

    @ApiOperation("首页分类导航(SKU上面)")
    @GetMapping("getsortList")
    public MindCraftResult getsortList() {


        return MindCraftResult.success(categoryService.list(
                new LambdaQueryWrapper<Category>()
                        .eq(Category::getBelong, CommonConst.CATEGORY_HOME)));

    }

    @ApiOperation("根据3级分类获取相同sku")
    @PostMapping("getCommonSku")
    public MindCraftResult getCommonSku(@RequestBody CommonSkuVo commonSkuVo) {
        //需要根，和skuid，查出用户点击的图片
        //据前端传入的分类id，查出用户所点击图片中三级分类的skuInfo

        return MindCraftResult.success(skuInfoService.getCommonSku(commonSkuVo));
    }


    @ApiOperation("根据id获取skuinfo")
    @GetMapping("getSkuById")
    public SkuInfo getSkuInfoById(@RequestParam("skuId")Integer skuId){
        SkuInfo skuInfo = skuInfoService.getById(skuId);

        SkuDetail skuDetail = skuDetailService.getOne(new LambdaQueryWrapper<SkuDetail>()
                .eq(SkuDetail::getSkuId, skuInfo.getId()));
        skuInfo.setUserId(skuDetail.getUserId());

        return skuInfo;
    }



}
